package com.zeny.edu.eduservice.controller;


import com.zeny.edu.common.ResponseResult;
import com.zeny.edu.eduservice.entity.EduSubject;
import com.zeny.edu.eduservice.service.EduSubjectService;
import com.zeny.edu.eduservice.entity.dto.SubjectNestedVo;
import com.zeny.edu.eduservice.service.RedisService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author zeny
 * @since 2020-02-29
 */
@RestController
@RequestMapping("/eduservice/subject")
@CrossOrigin
public class EduSubjectController {

    @Autowired
    private EduSubjectService eduSubjectService;

    @Autowired
    private RedisService redisService;

    /**
     * @param file
     * @return com.zeny.edu.common.ResponseResult
     * @Description 上传Excel文件-
     * @Date 2020/2/29 0029 23:55
     **/
    @PostMapping("/import")
    public ResponseResult importExcelSubject(@RequestParam(value = "file", required = false) MultipartFile file) {
        List<String> msg = eduSubjectService.importExcelSubject(file);
        return msg.size() == 0 ? ResponseResult.ok() : ResponseResult.fail().data("msgList", msg);
    }

    /**
     * @param
     * @return com.zeny.edu.common.ResponseResult
     * @Description 嵌套数据列表
     * @Date 2020/3/2 0002 15:21
     **/
    @ApiOperation(value = "嵌套数据列表")
    @GetMapping("/list")
    public ResponseResult nestedList() {
        List<SubjectNestedVo> subjectNestedVos = eduSubjectService.nestedList();
        return ResponseResult.ok().data("items", subjectNestedVos);
    }
    /**
     * @Description 删除子节点
     * @Date 2020/3/2 0002 16:37
     * @param
     * @return com.zeny.edu.common.ResponseResult
     **/
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable("id") String id) {
        boolean success = eduSubjectService.deleteById(id);
        return success ? ResponseResult.ok() : ResponseResult.fail().message("存在子节点，无法删除");
    }

    /**
     * @Description 新增一级分类
     * @Date 2020/3/2 0002 17:44
     * @param
     * @return com.zeny.edu.common.ResponseResult
     **/
    @ApiOperation(value = "新增一级分类")
    @PostMapping("/addOneLevel")
    public ResponseResult addOneLevel(@RequestBody EduSubject eduSubject) {
        if (StringUtils.isBlank(eduSubject.getTitle())) {
            return ResponseResult.fail().message("请输入分类名称");
        }
        boolean result = eduSubjectService.saveOneLevel(eduSubject);
        return result ? ResponseResult.ok() : ResponseResult.fail().message("该分类已存在");
    }

    /**
     * @Description 新增二级分类
     * @Date 2020/3/2 0002 17:44
     * @param
     * @return com.zeny.edu.common.ResponseResult
     **/
    @ApiOperation(value = "新增二级分类")
    @PostMapping("/addTwoLevel")
    public ResponseResult addTwoLevel(@RequestBody EduSubject eduSubject) {
        if (StringUtils.isBlank(eduSubject.getTitle())) {
            return ResponseResult.fail().message("请输入分类名称");
        }
        boolean result = eduSubjectService.saveTwoLevel(eduSubject);
        return result ? ResponseResult.ok() : ResponseResult.fail().message("该分类已存在");
    }
}

